package com.cos.hospital.tool;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;

import com.cos.hospital.R;
import com.cos.hospital.tool.glide.UGlide;

import uk.co.senab.photoview.PhotoView;
import uk.co.senab.photoview.PhotoViewAttacher;

/**
 * @Description: 展示大图
 * @Author: Kosmos
 * @Date: 2018年10月02日 18:01
 * @Email: KosmoSakura@foxmail.com
 * @Event:
 */
public class UPoPImages implements PhotoViewAttacher.OnPhotoTapListener {
    private Context act;
    private PopupWindow popup;
    private PhotoView iShow;

    /**
     * 外部静态调用
     */
    public static UPoPImages with(Context context) {
        UPoPImages uPoPImages = new UPoPImages(context);
        return uPoPImages;
    }

    private UPoPImages(Context context) {
        act = context;
        View rootView = LayoutInflater.from(context).inflate(R.layout.lay_dialog_show_image, null);
        iShow = rootView.findViewById(R.id.dia_image_show);
        iShow.setOnPhotoTapListener(this);
        popup = new PopupWindow(rootView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        popup.setBackgroundDrawable(new ColorDrawable(0));
        popup.setFocusable(false);
        popup.setOutsideTouchable(false);
    }

    private ViewGroup getView() {
        return (ViewGroup) ((Activity) act).findViewById(android.R.id.content);
    }

    public UPoPImages show(String url) {
        if (popup != null) {
            UGlide.loadPop(act, url, iShow);
            popup.showAtLocation(getView(), Gravity.BOTTOM, 0, 0);
        }
        return this;
    }

    public boolean isShow() {
        return popup != null && popup.isShowing();
    }

    public void hide() {
        if (popup != null && popup.isShowing()) {
            popup.dismiss();
        }
    }

    @Override
    public void onPhotoTap(View view, float x, float y) {
        hide();
    }

    @Override
    public void onOutsidePhotoTap() {
        hide();
    }
}
